package cn.wsn2018.tools.service;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.dao.ProtocolDesignDao;
import cn.wsn2018.tools.dao.UserDao;
import cn.wsn2018.tools.model.Design.ProtocolDesign;
import cn.wsn2018.tools.model.User;
import cn.wsn2018.tools.model.VO.ProtocolDesignView;
import cn.wsn2018.tools.util.JsonUtil;
import cn.wsn2018.tools.util.LogUtil;
import cn.wsn2018.tools.util.Regex;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;

/**
 * 用于处理用户协议字段查询，协议字段设计的业务逻辑类
 */

@Service
public class ProtocolDesignService {

    @Autowired
    private ProtocolDesignDao protocolDesignDao;

    @Autowired
    private UserDao userDao;

    private Logger logger=Logger.getLogger(ProtocolDesignService.class.getName());

    /**
     * 新建协议设计
     * @param protocolDesign
     * @param myUsername
     * @return
     */
    public String dealProtocolDesignSave(ProtocolDesign protocolDesign,String myUsername){
        if(protocolDesign!=null){
            //当前时间
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            sdf.setCalendar(new GregorianCalendar(new SimpleTimeZone(1000*60*60*8,"GMT")));
            protocolDesign.setDesignName(Regex.checkReg(protocolDesign.getDesignName()));
            protocolDesign.setOther(Regex.checkReg(protocolDesign.getOther()));
            protocolDesign.setUsername(myUsername);
            protocolDesign.setCreateTime(sdf.format(date));
            String id = protocolDesignDao.saveProtocolDesign(protocolDesign);
            if(id!=null){
                LogUtil.produceLogs(myUsername,CommonValue.CREATE_TYPE,CommonValue.CREATE_DESIGN_LOGINFO+",协议设计id为：D-"+(id.substring(6,8)+id.substring(18)).toUpperCase(),CommonValue.SUCCESS);
                JSONObject objectId = new JSONObject();
                objectId.put("id",id);
                return JsonUtil.successJson(CommonValue.JSON_INSERT_SUCCESS,objectId,null);
            }else {
                LogUtil.produceLogs(myUsername,CommonValue.CREATE_TYPE,CommonValue.CREATE_DESIGN_LOGINFO,CommonValue.FAILED);
                return JsonUtil.failedJson(CommonValue.JSON_INSERT_FAILED);
            }
        }else {
            LogUtil.produceLogs(myUsername,CommonValue.CREATE_TYPE,CommonValue.CREATE_DESIGN_LOGINFO,CommonValue.FAILED);
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

    }

    /**
     * 查询用户的全部协议设计
     * @param username
     * @return
     */
    public String dealProtocolDesignsQuery(String username,String pageNum,String pageSize){

        if(username!=null&&!username.isEmpty()){
            User user = userDao.getUserByName(username);
            if(user!=null){
                List<ProtocolDesign> designs = protocolDesignDao.getProtocolDesigns(username,pageNum,pageSize);
                if(designs!=null){
                    List<ProtocolDesignView> result = new ArrayList<>();
                    for(ProtocolDesign protocolDesign : designs){
                        ProtocolDesignView protocolDesignView = new ProtocolDesignView();
                        protocolDesignView.setId(protocolDesign.getId());
                        protocolDesignView.setUsername(protocolDesign.getUsername());
                        protocolDesignView.setProtocolName(protocolDesign.getProtocolName());
                        protocolDesignView.setDesignName(protocolDesign.getDesignName());
                        protocolDesignView.setCreateTime(protocolDesign.getCreateTime());
                        protocolDesignView.setOther(protocolDesign.getOther());
                        result.add(protocolDesignView);
                    }
                    JSONArray jsonArray = JSONArray.fromObject(result);
                    return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,null,jsonArray);
                }else {
                    return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
                }
            }else {
                return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
            }
        }else {
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

    }


    /**
     * 查询某个设计
     * @param id
     * @return
     */
    public String dealProtocolDesignQuery(String id){
        if(id!=null&&!id.isEmpty()){
            ProtocolDesign result = protocolDesignDao.getProtocolDesign(id);

            JSONObject jsonObject = JSONObject.fromObject(result);
            return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,jsonObject,null);
        }else{
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
    }

    /**
     * 修改用户设计的字段信息
     * @param protocolDesign
     * @return
     */
    public String dealProtocolDesignUpdate(ProtocolDesign protocolDesign,String myUsername){
        String id = protocolDesign.getId();
        if(protocolDesign!=null){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            protocolDesign.setUsername(myUsername);
            protocolDesign.setCreateTime(sdf.format(date));
            boolean result = protocolDesignDao.updateProtocolDesign(protocolDesign);

            if(result){
                LogUtil.produceLogs(myUsername,CommonValue.UPDATE_TYPE,CommonValue.UPDATE_DESIGN_LOGINFO+"，协议设计id：D-"+(id.substring(6,8)+id.substring(18)).toUpperCase(),CommonValue.SUCCESS);
                return JsonUtil.successJson(CommonValue.JSON_UPDATE_SUCCESS,null,null);
            }else {
                LogUtil.produceLogs(myUsername,CommonValue.UPDATE_TYPE,CommonValue.UPDATE_DESIGN_LOGINFO+"，协议设计id：D-"+(id.substring(6,8)+id.substring(18)).toUpperCase(),CommonValue.FAILED);
                return JsonUtil.failedJson(CommonValue.JSON_UPDATE_FAILED);
            }
        }else {
            LogUtil.produceLogs(myUsername,CommonValue.UPDATE_TYPE,CommonValue.UPDATE_DESIGN_LOGINFO+"，协议设计id：D-"+(id.substring(6,8)+id.substring(18)).toUpperCase(),CommonValue.FAILED);
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }

    }

    /**
     * 删除用户的某一协议设计
     */
    public String dealProtocolDesignDelete(String id,String myUsername){
        if(id!=null&&!id.isEmpty()){
            boolean result = protocolDesignDao.deleteProtocolDesign(id);
            if(result){
                LogUtil.produceLogs(myUsername,CommonValue.DELETE_TYPE,CommonValue.DELETE_DESIGN_LOGINFO+"，协议设计id：D-"+(id.substring(6,8)+id.substring(18)).toUpperCase(),CommonValue.SUCCESS);
                return JsonUtil.successJson(CommonValue.JSON_DELETE_SUCCESS, null,null);
            }else {
                LogUtil.produceLogs(myUsername,CommonValue.DELETE_TYPE,CommonValue.DELETE_DESIGN_LOGINFO+"，协议设计id：D-"+(id.substring(6,8)+id.substring(18)).toUpperCase(),CommonValue.FAILED);
                return JsonUtil.failedJson(CommonValue.JSON_DELETE_FAILED);
            }
        }else {
            LogUtil.produceLogs(myUsername,CommonValue.DELETE_TYPE,CommonValue.DELETE_DESIGN_LOGINFO+"，协议设计id：D-"+(id.substring(6,8)+id.substring(18)).toUpperCase(),CommonValue.FAILED);
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
    }

    /**
     * 批量删除协议设计
     * @param idList
     * @return
     */
    public String dealProtocolDesignsDelete(List<String> idList,String myUsername){
        int num = idList.size();
        if(idList!=null&&num!=0){
            int count = protocolDesignDao.deleteProtocolDesigns(idList);
            if(count==num){
                LogUtil.produceLogs(myUsername,CommonValue.DELETE_TYPE,CommonValue.DELETE_DESIGN_LOGINFO+num+"条",CommonValue.SUCCESS);
                return JsonUtil.successJson(CommonValue.JSON_DELETE_SUCCESS+",计划删除"+num+
                        "条,实际删除"+count+"条",null,null);
            }else {
                LogUtil.produceLogs(myUsername,CommonValue.DELETE_TYPE,CommonValue.DELETE_DESIGN_LOGINFO+num+"条",CommonValue.FAILED);
                return JsonUtil.failedJson(CommonValue.JSON_DELETE_FAILED+",计划删除"+num+
                        "条,实际删除"+count+"条");
            }
        }else {
            LogUtil.produceLogs(myUsername,CommonValue.DELETE_TYPE,CommonValue.DELETE_DESIGN_LOGINFO+num+"条",CommonValue.FAILED);
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
    }

    /**
     * 查询协议设计有哪些功能
     * @param id
     * @return
     */
    public String dealProtocolDesignFunctions(String id){
        if(id!=null&&!id.isEmpty()){
            ProtocolDesign protocolDesign = protocolDesignDao.getProtocolDesign(id);
            if(protocolDesign!=null){
                List<String> funcList = new ArrayList<>();
                if(protocolDesign.getAuth()!=null){
                    funcList.add(CommonValue.FUNCTION_AUTHNEGO);
                }
                if(protocolDesign.getEncrypt()!=null){
                    funcList.add(CommonValue.FUNCTION_ENCRYPT);
                }
                if(protocolDesign.getException()!=null){
                    funcList.add(CommonValue.FUNCTION_EXCEPTION);
                }
                if(protocolDesign.getVirus()!=null){
                    funcList.add(CommonValue.FUNCTION_VIRUS);
                }
                if(protocolDesign.getResend()!=null){
                    funcList.add(CommonValue.FUNCTION_RESEND);
                }
                if(protocolDesign.getIntegrity()!=null){
                    funcList.add(CommonValue.FUNCTION_INTEGRITY);
                }
                JSONArray jsonArray = JSONArray.fromObject(funcList);
                return JsonUtil.successJson(CommonValue.JSON_QUERY_SUCCESS,null,jsonArray);
            }else {
                return JsonUtil.failedJson(CommonValue.JSON_QUERY_FAILED);
            }
        }else{
            return JsonUtil.failedJson(CommonValue.JSON_INPUT_ERROR);
        }
    }

}
